let logger = require("pomelo-logger").getLogger(__filename);

function compareTable(t1, t2) {
    if (t1.getNumOfPlayers() - t2.getNumOfPlayers() < 0) {
        return -1;
    } else if (t1.getNumOfPlayers() - t2.getNumOfPlayers() > 0) {
        return 1;
    }
    return t1.getCreateTime() - t2.getCreateTime() > 0 ? 1 : -1;
}

module.exports.getYuanBaoTables = function (service, rid) {
    if (!rid) {
        return {resultCode: 404, message: "invalid rid"};
    }
    let yuanBaoTables = service.getVisiblePlayerYuanBaoTablesByRid(rid);
    yuanBaoTables.sort(compareTable);
    let serializedTables = [];
    yuanBaoTables.forEach(function (table) {
        serializedTables.push(table.toYuanBaoTable());
    });
    return {resultCode: 0, message: "ok", tables: serializedTables};
};

module.exports.quickJoinYuanBaoTable = function (app, service, msg, session, callback) {
    let result = service.startQuickJoin(session.frontendId, session.id, msg.rid);
    if (result.resultCode !== 0) {
        callback(null, {resultCode: result.resultCode, message: result.message});
        return;
    }
    app.rpc.account.tableRemote.loginGame(null, {
        frontendId: session.frontendId,
        sessionId: session.id,
        minStack: result.table.getRoom().minStack,
        maxStack: result.table.getRoom().maxStack,
        gid: service.getGid(),
        action: "join"
    }, function (response) {
        callback(null, service.finishJoin(result.table, response, session.frontendId, session.id));
    });
};

module.exports.joinYuanBaoTable = function (app, service, msg, session, callback) {
    let result = service.startJoin(session.frontendId, session.id, msg.code);
    if (result.resultCode !== 0) {
        callback(null, {
            resultCode: result.resultCode,
            message: result.message
        });
        return;
    }
    let room = result.table.getRoom();
    let gid = service.getGid();
    app.rpc.account.tableRemote.loginGame(null, {
        gid: gid,
        frontendId: session.frontendId,
        sessionId: session.id,
        minStack: room.minStack,
        maxStack: result.table.getRoom().maxStack,
        action: "join"
    }, function (response) {
        callback(null, service.finishJoin(result.table, response, session.frontendId, session.id));
    });
};

module.exports.createYuanBaoTable = function (app, service, msg, session, callback, dataCreator) {
    let result = service.startCreateTable(msg, session.frontendId, session.id);
    if (result.resultCode !== 0) {
        logger.info("callback(null, {resultCode: result.resultCode, message: result.message});");
        callback(null, {resultCode: result.resultCode, message: result.message});
        return;
    }
    let room = result.room;
    app.rpc.account.tableRemote.loginGame(null, {
        frontendId : session.frontendId,
        sessionId : session.id,
        gid: service.getGid(),
        minStack: room.minStack,
        maxStack: room.maxStack,
        action: "create"
    }, function (response) {
        let data = dataCreator(msg);
        data["code"] = response.code;
        data["room"] = room;
        data["creatorUid"] = response.account ? response.account.uid : 0;
        data["gid"] = service.getGid();
        callback(null, service.finishCreateTable(data, response,
            session.frontendId, session.id));
    });
};

module.exports.leaveTable = function (service, app, session, callback) {
    let result = service.leaveTable(session.frontendId, session.id);
    if (result.resultCode === 0) {
        app.rpc.account.tableRemote.logoutGame(null, {
            frontendId: session.frontendId,
            sessionId: session.id,
            gid: service.getGid()
        }, function () {

        });
    }
    callback(null, result);
};